package com.gitee.liuzhi.common.util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;

import java.util.Map;

/**
 * @Author liuzhi
 * @Date 2023/8/29 22:10
 * @Version 1.0
 * @Description Map 工具类
 */
public class MapUtil {

    /**
     * 将一个对象转换为Map集合
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key.toString(), beanMap.get(key));
            }
        }
        return map;
    }


    /**
     * 将一个map转换为对象
     */
    public static <T> T mapToBean(Map source, Class<T> target) {
        return JSON.parseObject(JSON.toJSONString(source), target);
    }

}
